<?php

namespace Biz\Play;

use Base\Exception\NoRecordException;

class Showings extends \Base\BaseBiz
{
    const STATUS_WAIT = 1;
    const STATUS_ACTIVE = 2;
    const STATUS_FINISH = 3;

    const TYPE_A = 1;
    const TYPE_B = 2;
    const TYPE_C = 3;
    const TYPE_D = 4;
    const TYPE_E = 5;
    const TYPE_F = 6;
    const TYPE_G = 7;
    const TYPE_H = 8;
    const TYPE_EIGHT = 9;
    const TYPE_FOUR = 10;
    const TYPE_SEMI = 11;
    const TYPE_COPPER = 12;
    const TYPE_FINAL = 13;

    const TYPE_ARR = array(
        self::TYPE_A => 'A组比赛',
        self::TYPE_B => 'B组比赛',
        self::TYPE_C => 'C组比赛',
        self::TYPE_D => 'D组比赛',
        self::TYPE_E => 'E组比赛',
        self::TYPE_F => 'F组比赛',
        self::TYPE_G => 'G组比赛',
        self::TYPE_H => 'H组比赛',
        self::TYPE_EIGHT => '八分之一决赛',
        self::TYPE_FOUR => '四分之一决赛',
        self::TYPE_SEMI => '半决赛',
        self::TYPE_COPPER => '三四名决赛',
        self::TYPE_FINAL => '决赛'
    );

    const STATUS_ARR = array(
        self::STATUS_WAIT => '等待开始',
        self::STATUS_ACTIVE => '正在进行',
        self::STATUS_FINISH => '比赛结束'
    );

    //状态标识
    public static function getStatusStr($tag){
        return self::STATUS_ARR[$tag]??'';
    }

    //类别标识
    public static function getTypeStr($tag){
        return self::TYPE_ARR[$tag]??'';
    }

    //添加信息
    public static function insert($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $showing_orm = new \Orm\PlayShowings\PlayShowingsClass();
            $showing_orm['type'] = $_orm['type']??'';
            $showing_orm['player_a'] = $_orm['player_a']??'';
            $showing_orm['player_b'] = $_orm['player_b']??'';
            $showing_orm['start_date'] = $_orm['start_date']??'';
            $showing_orm['start_time'] = $_orm['start_time']??'';
            $showing_orm['play_number'] = $_orm['play_number']??'';
            $showing_orm['open_time'] = $_orm['open_time']??'';
            $showing_orm['stop_time'] = $_orm['stop_time']??'';
            $showing_orm['score_a'] = $_orm['score_a']??'';
            $showing_orm['score_b'] = $_orm['score_b']??'';
            $showing_orm['status'] = $_orm['status']??'';
            $showing_orm['exec_week_score'] = $_orm['exec_week_score']??0;
            $showing_orm['week_own'] = $_orm['week_own']??1;

            $showing_orm->insert();
            if($showing_orm['id']){
                $rs['isok'] = true;
            }else{
                throw new \Exception('新增数据失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    //更新信息
    public static function update($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $showing_orm = new \Orm\PlayShowings\PlayShowingsClass();
            if(!isset($_orm['id']) || !$_orm['id']){
                throw new \Exception('数据错误');
            }
            $showing_orm['id'] = $_orm['id'];
            $showing_orm['type'] = $_orm['type']??'';
            $showing_orm['player_a'] = $_orm['player_a']??'';
            $showing_orm['player_b'] = $_orm['player_b']??'';
            $showing_orm['start_date'] = $_orm['start_date']??'';
            $showing_orm['start_time'] = $_orm['start_time']??'';
            $showing_orm['play_number'] = $_orm['play_number']??'';
            $showing_orm['open_time'] = $_orm['open_time']??'';
            $showing_orm['stop_time'] = $_orm['stop_time']??'';
            $showing_orm['score_a'] = $_orm['score_a']??'';
            $showing_orm['score_b'] = $_orm['score_b']??'';
            $showing_orm['status'] = $_orm['status']??'';
            $showing_orm['exec_week_score'] = $_orm['exec_week_score']??0;
            $showing_orm['week_own'] = $_orm['week_own']??1;

            $is_update = $showing_orm->update();
            if($is_update){
                $rs['isok'] = true;
            }else{
                throw new \Exception('更新失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    /**
     * 获取信息
     */
    public static function getInfo($id)
    {
        $showing_orm = new \Orm\PlayShowings\PlayShowingsClass();
        $showing_orm->loadById($id);

        return $showing_orm;
    }

    /*
     * 获取列表
     */
    public static function getList($where, $now = 1, $size = 0)
    {
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $showing_list = new \Orm\PlayShowings\PlayShowingsList();
        if (!empty($where)) {
            $showing_list['where'] = $where;
        } else {
            $showing_list['where'] = '';
        }
        $showing_list['limit'] = $size;
        $showing_list['offset'] = ($now - 1) * $size;
        $showing_list['order'] = '`play_number` ASC';
        $showing_list['key_by'] = 'id';
        $showing_list->load();

        $result = array();
        $result['lists'] = $showing_list['lists'];
        $result['total'] = $showing_list['total'];
        return $result;
    }

    /**
     * @param $week_own
     * @return mixed
     * 根据周次查询本周是否有未结束的赛事信息
     */
    public static function getPlayShowingByWeekOwn($week_own){
        $model = new \Orm\PlayShowings\PlayShowingsModel();
        return $model->getPlayShowingByWeekOwn($week_own);
    }

    /**
     * @return mixed
     * 查询所有赛事是否已经全部结束
     */
    public function getPlayShowing(){
        $model = new \Orm\PlayShowings\PlayShowingsModel();
        return $model->getPlayShowing();
    }
}
